package com.openxt.nbzj.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.openxt.nbzj.R;
import com.openxt.nbzj.activity.WebViewActivity;
import com.openxt.nbzj.model.ConfigModel;

import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import qs.openxt.libs.util.ActivityUtil;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016/5/31.
 */
public class ActivityFragment extends OpenxtFragment implements View.OnClickListener {
    @BindView(R.id.imageView)
    ImageView imageView;

    private JSONObject data;
    public void setJSON(JSONObject data){
        this.data = data;
    }
    private Context mContext = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fragment, container, false);
        ButterKnife.bind(this, view);
        imageView.setOnClickListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(data == null)
            return;
        mContext = getActivity();
        String IMGURL = data.getString("IMGURL");
        if(StringUtil.isNull(IMGURL))
            return;
        x.image().bind(imageView, IMGURL);
    }

    @Override
    public void onClick(View v) {
        if(!ActivityUtil.isNetworkAvailable(mContext)){
            showMsg("无网络，请先查看网络是否正常打开");
            return;
        }
        String RedirectURL = data.getString("URL");
        if(StringUtil.isNull(RedirectURL))
            return;
        Utils.setLocalStorage(mContext, ConfigModel.NATIVE_PAGENAME, getActionUrl());
        Bundle bundle = new Bundle();
        bundle.putString("url", ConfigModel.ROOTPATH + "index.html" + RedirectURL);
        toActivity(WebViewActivity.class, bundle, false);
    }

    @Override
    public void refresh() {

    }

    @Override
    public void willBeDisplayed() {

    }
}
